#include "test_alarm.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"

//闹钟名称，4个字符，用户可自定义，更改宏中的字符即可
#define TEST_ALARM ((unsigned)('O') | ((unsigned)('C') << 8) | ((unsigned)('A') << 16) | ((unsigned)('L') << 24))
//单次闹钟序号
#define SINGLE_ALARM  0
//重复闹钟序号
#define REPEATE_ALARM 1
//闹钟到期回调函数
static void cm_test_alarm_cb(struct drvRtcAlarm *alarm, void *ctx)
{
    cm_demo_printf("owner:%d,index:%d,context:%s\n",alarm->owner,alarm->name,ctx);
	//根据闹钟序号来分辨到期的闹钟具体是哪个
    if(alarm->name == REPEATE_ALARM)
    {
        cm_demo_printf("[RP_ALARM]:EXPIRE\n");
    }
    if(alarm->name == SINGLE_ALARM)
    {
        cm_demo_printf("[SG_ALARM]:EXPIRE\n");
    }
}

void cm_test_alarm_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("SET_CB",cmd[2],strlen("SET_CB")))
    {
        cm_demo_printf("drvRtcAlarmOwnerSetCB ret:%d\n",drvRtcAlarmOwnerSetCB(TEST_ALARM, "test alarm", cm_test_alarm_cb));
    }else if(0 == strncmp("SET_SG",cmd[2],strlen("SET_SG")))
    {
        //获取当前时刻，单位秒
        long curr = osiEpochSecond(); 
        //设置闹钟，时间间隔使用命令传入的参数，如果当前闹钟已存在，则强制替换为当前参数
        cm_demo_printf("drvRtcSetAlarm ret:%d\n",drvRtcSetAlarm(TEST_ALARM,SINGLE_ALARM,0,0,curr+atoi(cmd[3]),true));
        cm_demo_printf("[SG_ALARM]:%ds\n",atoi(cmd[3]));
    }
    else if(0 == strncmp("SET_SG",cmd[2],strlen("SET_SG")))
    {
        //获取当前时刻，单位秒
        long curr = osiEpochSecond(); 
        //设置闹钟，时间间隔使用命令传入的参数，如果当前闹钟已存在，则强制替换为当前参数
        cm_demo_printf("drvRtcSetAlarm ret:%d\n",drvRtcSetAlarm(TEST_ALARM,SINGLE_ALARM,"opaque info",11,curr+atoi(cmd[3]),true));
        cm_demo_printf("[SG_ALARM]:%ds\n",atoi(cmd[3]));
    }else if(0 == strncmp("SET_RP",cmd[2],strlen("SET_RP")))
    {
        //星期掩码，从低到高依次是星期日，星期一...星期六
        unsigned char day_mask = 0;// = (1)|(1<<6)|(1<<3); //星期天、星期六、星期三
        int i;
        int hour,min,sec;
        int total_sec;
        //检查参数个数，超过范围则退出
        if((len > 13)||(len < 6)) //最多12个参数，最少6个参数，星期:时:分:秒
        {
            cm_demo_printf("[RP_ALARM]param error\n");
            return;
        }
        //提取星期参数，最少一个，最多七个
        for(i = 3;i< len-3;i++)
        {
            day_mask |=(unsigned char) (1<< atoi(cmd[i]));
            cm_demo_printf("[RP_ALARM]day %d set done\n",atoi(cmd[i]));
        }
        cm_demo_printf("[RP_ALARM]day mask %d set done\n",day_mask);
        //提取时分秒信息
        hour = atoi(cmd[len-3]);
        min = atoi(cmd[len-2]);
        sec = atoi(cmd[len-1]);
        //将时分秒信息转换为秒
        total_sec = hour*60*60 + min*60 + sec;
        cm_demo_printf("[RP_ALARM]hour:%d,min:%d,sec%d\n",hour,min,sec);
        //设置重复闹钟，当该闹钟已存在，则强制替换为当前参数
        cm_demo_printf("drvRtcSetRepeatAlarm ret:%d\n",drvRtcSetRepeatAlarm(TEST_ALARM, REPEATE_ALARM, "RP opaque info",14, day_mask,total_sec, cm_sys_get_timezone() * 15*60, true));
    }else if(0 == strncmp("RM_ONE",cmd[2],strlen("RM_ONE")))
    {
        uint32_t owner,name;
        owner = cmd[3][0]<<24 | cmd[3][1]<<16 | cmd[3][2]<<8 | cmd[3][3]; 
        name = atoi(cmd[4]);
        cm_demo_printf("drvRtcRemoveAlarm ret: %d\n",drvRtcRemoveAlarm(owner, name));
    }else if(0 == strncmp("GET_ALARM_INFO",cmd[2],strlen("GET_ALARM_INFO")))
    {
        //分配那种参数结构体
        drvRtcAlarm_t alarms[2];
        //清零参数
        memset(alarms,0,sizeof(drvRtcAlarm_t)*2);
        //获取当前闹钟参数
        cm_demo_printf("drvRtcGetAlarms ret:%d\n",drvRtcGetAlarms(TEST_ALARM, alarms, 2));
        //打印闹钟参数
        cm_demo_printf("owner:%d,index:%d,type:%d,mask:%#x,expire:%d,info:%s\n",alarms[0].owner,alarms[0].name,alarms[0].type,alarms[0].wday_repeated,alarms[0].expire_sec,alarms[0].info);
        cm_demo_printf("owner:%d,index:%d,type:%d,mask:%#x,expire:%d,info:%s\n",alarms[0].owner,alarms[1].name,alarms[1].type,alarms[1].wday_repeated,alarms[1].expire_sec,alarms[0].info);
    }
    else if(0 == strncmp("GET_COUNT",cmd[2],strlen("GET_COUNT")))
    {
        uint32_t owner,name;
        owner = cmd[3][0]<<24 | cmd[3][1]<<16 | cmd[3][2]<<8 | cmd[3][3]; 
        cm_demo_printf("drvRtcGetAlarmCount ret: %d\n", drvRtcGetAlarmCount(owner));
    }
    else if(0 == strncmp("RM_ALL",cmd[2],strlen("RM_ALL")))
    {
        drvRtcRemoveAllAlarms();
    }
    else{
        cm_demo_printf("param error!\n");
    }
}

